package cloud.maque.sms.service;

import cloud.maque.sms.api.dto.SmsDTO;
import cloud.maque.sms.api.dto.SmsResponse;
import cloud.maque.sms.api.provider.SmsProvider;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class SmsSendService {

    private final Map<String, SmsProvider> providerMap;

    public SmsSendService(List<SmsProvider> providers) {
        this.providerMap = providers.stream()
                .collect(Collectors.toMap(SmsProvider::getProviderName, Function.identity()));
    }

    public SmsResponse send(String provider, SmsDTO request) {
        SmsProvider smsProvider = providerMap.get(provider);
        if (smsProvider == null) {
            return SmsResponse.builder()
                    .success(false)
                    .code("PROVIDER_NOT_FOUND")
                    .message("SMS provider not found: " + provider)
                    .build();
        }
        return smsProvider.send(request);
    }
}
